package com.demo.rocketmq.consumer;

import com.alibaba.fastjson.JSON;
import com.demo.rocketmq.entity.User;
import com.demo.rocketmq.annotation.MqConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 测试注解@MqConsumer
 * @author cyj
 */
@Slf4j
@Service
public class AnnotationMqConsumer {

    /**
     * @MqConsumer提供三个属性
     * topic=消费的主题名
     * consumerGroup=消费者组
     * selectorExpression=TAG标签，用于同一主题下区分不同类型的消息
     * @param message 消息
     */
    @MqConsumer(topic = "test-topic-1", consumerGroup = "mq_consumer_group1")
    public void consumerTestTopic1(String message) {
        log.info("mq consumer received topic=test-topic-1 message: {}", message);
    }

    @MqConsumer(topic = "test-topic-1", consumerGroup = "mq_consumer_group1_1", callbackTopic = "test-topic-1")
    public void consumerTestTopic1AndCallback(String message) {
        log.info("mq consumer received topic=test-topic-1 and callback, message: {}", message);
    }

    @MqConsumer(topic = "test-topic-1", consumerGroup = "mq_consumer_group1_2", selectorExpression = "tag")
    public void consumerTestTopic1ByTag(String message) {
        log.info("mq consumer received topic=test-topic-1, selectorExpression=tag message: {}", message);
    }

    @MqConsumer(topic = "test-topic-2", consumerGroup = "mq_consumer_group2")
    public void consumerTestTopic2(User user) {
        log.info("mq consumer received message: {}", JSON.toJSONString(user));
    }
}
